﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package zasp.Util {

    public class CircularBuffer {

        private var arr:Array = null;
        private var ins:int = 0;
        private var num:int = 0;

        public function CircularBuffer(_arg1:int){
            this.arr = new Array(_arg1);
            this.ins = 0;
            this.num = 0;
        }
        public static function test():void{
            var happy:* = true;
            var buf:* = null;
            var str1:* = "string1";
            var str2:* = "string2";
            var pop1:* = null;
            var pop2:* = null;
            buf = new CircularBuffer(0);
            if (0 != buf.capacity){
                throw (new Error("a1"));
            };
            if (0 != buf.length){
                throw (new Error("b1"));
            };
            try {
                buf.push(10);
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("c1"));
            };
            try {
                buf.pop();
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("d1"));
            };
            buf = new CircularBuffer(1);
            if (1 != buf.capacity){
                throw (new Error("a2"));
            };
            if (0 != buf.length){
                throw (new Error("b2"));
            };
            buf.push(str1);
            try {
                buf.push(10);
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("c2"));
            };
            pop1 = buf.pop();
            if (str1 != pop1){
                throw (new Error("d2"));
            };
            try {
                buf.pop();
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("e2"));
            };
            buf = new CircularBuffer(2);
            if (2 != buf.capacity){
                throw (new Error("a3"));
            };
            if (0 != buf.length){
                throw (new Error("b3"));
            };
            buf.push(str1);
            pop1 = buf.pop();
            if (str1 != pop1){
                throw (new Error("c3"));
            };
            buf.push(str2);
            pop2 = buf.pop();
            if (str2 != pop2){
                throw (new Error("d3"));
            };
            buf.push(str1);
            buf.push(str2);
            try {
                buf.push(3);
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("e3"));
            };
            pop1 = buf.pop();
            pop2 = buf.pop();
            if (str1 != pop1){
                throw (new Error("f3"));
            };
            if (str2 != pop2){
                throw (new Error("g3"));
            };
            try {
                buf.pop();
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("h3"));
            };
            buf = new CircularBuffer(2);
            if (2 != buf.capacity){
                throw (new Error("a4"));
            };
            if (0 != buf.length){
                throw (new Error("b4"));
            };
            buf.push(str1);
            pop1 = buf.pop();
            if (str1 != pop1){
                throw (new Error("c4"));
            };
            buf.push(str2);
            pop2 = buf.pop();
            if (str2 != pop2){
                throw (new Error("d4"));
            };
            buf.push(str1);
            buf.push(str2);
            try {
                buf.push(3);
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("e4"));
            };
            var len:* = buf.length;
            buf.capacity = (1 + buf.capacity);
            if (buf.length != len){
                throw (new Error("len4.1"));
            };
            buf.push(3);
            if (str1 != buf.pop()){
                throw (new Error("f4.a"));
            };
            if (str2 != buf.pop()){
                throw (new Error("f4.b"));
            };
            if (3 != buf.pop()){
                throw (new Error("f4.c"));
            };
            buf.push(1);
            buf.push(2);
            try {
                buf.capacity = 1;
                happy = false;
            } catch(expected:Error) {
            };
            if (!happy){
                throw (new Error("g4"));
            };
        }

        public function get capacity():int{
            return (this.arr.length);
        }
        public function set capacity(_arg1:int):void{
            var _local2:CircularBuffer = new CircularBuffer(_arg1);
            while (this.length > 0) {
                _local2.push(this.pop());
            };
            this.arr = _local2.arr;
            this.ins = _local2.ins;
            this.num = _local2.num;
        }
        public function get length():int{
            return (this.num);
        }
        public function push(_arg1:Object):void{
            if (this.num >= this.arr.length){
                throw (new Error("overflow"));
            };
            this.arr[this.ins] = _arg1;
            this.num = (this.num + 1);
            this.ins = ((this.ins + 1) % this.arr.length);
        }
        public function pop():Object{
            if (this.num <= 0){
                throw (new Error("underflow"));
            };
            var _local1:int = (((this.arr.length + this.ins) - this.num) % this.arr.length);
            var _local2:Object = this.arr[_local1];
            this.arr[_local1] = 0;
            this.num = (this.num - 1);
            return (_local2);
        }
        public function elementAt(_arg1:int):Object{
            if ((((_arg1 < 0)) || ((_arg1 >= this.num)))){
                throw (new Error("out of range"));
            };
            var _local2:int = ((((this.arr.length + this.ins) - this.num) + _arg1) % this.arr.length);
            return (this.arr[_local2]);
        }

    }
}//package zasp.Util 
